package com.zc.im.common.r2dbc.event.callback;

import com.zc.im.common.r2dbc.domain.AbstractBase;
import com.zc.im.common.util.SnowFlakeUtil;
import org.reactivestreams.Publisher;
import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.r2dbc.mapping.event.BeforeSaveCallback;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.r2dbc.core.Parameter;
import reactor.core.publisher.Mono;

/**
 * Project：im
 * Date：2022/4/20
 * Time：10:48
 * Description：保存前事件回调
 *
 * @author Challen.Zhang
 * @version 1.0
 */
public class BaseBeforeSaveCallback implements BeforeSaveCallback<AbstractBase> {
    @Override
    public Publisher<AbstractBase> onBeforeSave(AbstractBase entity, OutboundRow row, SqlIdentifier table) {
        if (entity.isNew()) {
            //  id生成
            long id = SnowFlakeUtil.snowflakeId();
            entity.setId(id);
            row.append("id", Parameter.from(id));
            entity.setNewObj(false);
        }
        return Mono.just(entity);
    }
}
